﻿using System.ComponentModel;
using System.IO;
using System.Windows.Controls;
using System.Windows.Media;

namespace FileCopyExtensions
{
    /// <summary>
    /// Logique d'interaction pour UI_WPF_FileCopyExtensions.xaml
    /// </summary>
    public partial class UI_WPF_FileCopyExtensions : UserControl
    {
        // declaration ---------------------------------------------------------------------------------------

        CopyFileCallbackAction copyAction;
        //private static string progressBarNormal = "#FF01D328";
        //private static string progressBarCancel = 
        private static Brush pgrNormal;
        private static Brush pgrCancel = Brushes.Yellow;
        private static Brush pgrStop = Brushes.Gray;
        private static Brush pgrErreur = Brushes.Red;
        private static Brush pgrSuccess = Brushes.Blue;

        CopyResult copyState;
        public CopyResult CopyState
        {
            get { return copyState; }
            set { copyState = value; }
        }

        bool vertical;
        [Category("Configuration"), Browsable(true), Description("Set the disposition vertical or hoirizontal")]
        public bool Vertical
        {
            get { return vertical; }
            set
            {
                vertical = value;
                if (vertical)
                {
                    DockPanel.SetDock(lbl_pourcentage, Dock.Bottom);
                }
                else
                {
                    DockPanel.SetDock(lbl_pourcentage, Dock.Right);
                }
            }
        }

        bool showProgress;
        [Category("Configuration"), Browsable(true), Description("Set the progress label visible or not")]
        public bool ShowProgress
        {
            get { return showProgress; }
            set
            {
                showProgress = value;
                if (showProgress)
                {
                    lbl_pourcentage.Visibility = System.Windows.Visibility.Visible;
                }
                else
                    lbl_pourcentage.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        FileInfo ficSource, folderDestination;

        public FileInfo FolderDestination
        {
            get { return folderDestination; }
            set { folderDestination = value; }
        }

        public FileInfo FileSource
        {
            get { return ficSource; }
            set { ficSource = value; }
        }

        string fileDestName;

        public string FileDestName
        {
            get { return fileDestName; }
            set { fileDestName = value; }
        }

        private event CopyFileCallback CopyProgress;
        private event CopyFileTerminate CopyEnd;

        // Delegates -----------------------------------------------------------------------------------------
        public delegate void MiseAJourProgress_delegate(double value);

        // Enum ----------------------------------------------------------------------------------------------

        // Fonctions ------------------------------------------------------------------------------------------

        public UI_WPF_FileCopyExtensions()
        {
            InitializeComponent();

            Vertical = true;
            ShowProgress = true;
            FileSource = null;
            FolderDestination = null;
            FileDestName = null;

            string colorHexa = "#FF01D328";
            TypeConverter converter = TypeDescriptor.GetConverter(typeof(Brush));
            pgrNormal = (Brush)converter.ConvertFrom(colorHexa);
        }

        /// <summary>
        /// Lance la copie des fichiers
        /// </summary>
        /// <param name="CopyOption">The operation to perform when starting copying</param>
        public void runCopy(CopyFileOptions CopyOption = CopyFileOptions.All)
        {
            string FileDest;
            copyAction = CopyFileCallbackAction.Continue;
            pgr.Foreground = pgrNormal;
            CopyProgress += new CopyFileCallback(CopyProgress_Handler);
            CopyEnd += new CopyFileTerminate(CopyEnd_Handler);

            if (FileDestName == null)
            {
                FileDestName = FileSource.Name;

            }

            if (FolderDestination.Extension == "")
            {
                FileDest = FolderDestination.FullName + "\\" + FileDestName;
            }
            else
            {
                FileDest = FolderDestination.FullName;
            }

            FileCopyExtensions.CopyFile(FileSource, new FileInfo(FileDest), CopyOption, CopyProgress, CopyEnd);
        }

        /// <summary>
        /// Cancel the copy in progress.
        /// The copy is stop and can't be restart
        /// </summary>
        public void cancelCopy()
        {
            copyAction = CopyFileCallbackAction.Cancel;
        }

        /// <summary>
        /// Stop the copy. Restart is possible.
        /// </summary>
        public void stopCopy()
        {
            copyAction = CopyFileCallbackAction.Stop;
        }


        /// <summary>
        /// Handle soulevé à chaque pas de progression de la copie
        /// </summary>
        /// <param name="source">Fichier source</param>
        /// <param name="destination">Dossier de destination</param>
        /// <param name="state">Etat</param>
        /// <param name="totalFileSize">taille du fichier</param>
        /// <param name="totalBytesTransferred">nombre de bits transféré</param>
        /// <returns></returns>
        private CopyFileCallbackAction CopyProgress_Handler(FileInfo source, FileInfo destination, object state, long totalFileSize, long totalBytesTransferred)
        {
            //progressBar1.Value = (double)totalBytesTransferred / (double)totalFileSize * 100;
            Dispatcher.Invoke(new MiseAJourProgress_delegate(MiseAJourProgress), (double)totalBytesTransferred / (double)totalFileSize * 100);
            //Console.WriteLine("state = " + state);

            return copyAction;
        }

        /// <summary>
        /// Handle soulevé à la fin de la copie
        /// </summary>
        /// <param name="success"></param>
        private void CopyEnd_Handler(bool success)
        {
            if (success)
            {
                pgr.Foreground = pgrSuccess;
                copyState = CopyResult.CopyTerminateOk;
            }
            else if (copyAction == CopyFileCallbackAction.Cancel)
            {
                pgr.Foreground = pgrCancel;
                copyState = CopyResult.CopyCancel;
            }
            else if (copyAction == CopyFileCallbackAction.Stop)
            {
                pgr.Foreground = pgrStop;
                copyState = CopyResult.CopyStop;
            }
            else
            {
                pgr.Foreground = pgrErreur;
                copyState = CopyResult.CopyError;
            }
        }

        /// <summary>
        /// Mise à jour de l'affichage
        /// </summary>
        /// <param name="value"></param>
        private void MiseAJourProgress(double value)
        {
            pgr.Value = value;
            lbl_pourcentage.Content = value.ToString("F2") + " %";
        }


    }
}
